package com.china08.yxyapi.api.manage.tec.teaching.lecture.res;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;

@CrossOrigin
@RestController
public class LectureResBookContentsKnoledgesSrhController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private YxyQuesKnowRepository yxyQuesKnowRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/res/book/bookcontent/knoledges/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel req) {
        String bookcontentsId = req.bookcontentsId;

        Assert.notNull(StringUtils.defaultIfBlank(bookcontentsId, null), "章节ID为空");

        List<YxyResBookContentsKnoledges> list = yxyResBookContentsKnoledgesRepository
                .findByBookContentId(req.bookcontentsId);
        List<Integer> knowList = new ArrayList<Integer>();

        for (YxyResBookContentsKnoledges yxyResBookContentsKnoledges : list) {
            knowList.add(yxyResBookContentsKnoledges.getKnowledgeId());
        }
        List<YxyQuesKnow> idIn = yxyQuesKnowRepository.findByIdIn(knowList);
        List<RespModel> knoledges = new ArrayList<>();
        for (YxyQuesKnow yxyQuesKnow : idIn) {
            RespModel model = new RespModel();
            model.knoledges = yxyQuesKnow.getName();
            model.knoledgesId = yxyQuesKnow.getId();
            knoledges.add(model);
        }
        return knoledges;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String bookcontentsId;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String knoledges;

        public Integer knoledgesId;
    }

}
